package com.topgay.dao;

import java.util.HashMap;
import java.util.List;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.topgay.domain.ChattingMessage;

@Repository
public class ChattingDAOImple implements ChattingDAO{
	
	@Autowired
	private SqlSessionTemplate session;
	
	public int createTimeLineSeq(String timeLineName){
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("timeLineName", timeLineName);
		return session.update("chatting.createTimeLineSeq", map);
	}
	
	public int insertMessageToChatting(ChattingMessage message){
		session.insert("chatting.insertMessageToChatting", message);
		
		return message.getChattingSeq(); 
	}
	
	public int insertMessageToTimeLine(ChattingMessage message){
		return session.insert("chatting.insertMessageToTimeLine", message);
	}
	
	public List<ChattingMessage> selectChattingMessageById(String timeLineName, String fromId){
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("timeLineName", timeLineName);
		map.put("fromId", fromId);
		List<ChattingMessage> list =  session.selectList("chatting.selectChattingMessageById", map);
		for(ChattingMessage message : list){
			session.delete("chatting.deleteChattingMessage", message.getChattingSeq());
		}
		
		return list;
	}
	
	public List<ChattingMessage> selectTimeLineMessageById(String timeLineName){
		
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("timeLineName", timeLineName);
		List<ChattingMessage> tmpList =  session.selectList("chatting.selectTimeLineMessageById", map);
		
		return tmpList;
	}
	
	public int selectTimeLineByName(String timeLineName){
		return session.selectOne("chatting.selectTimeLineByName", timeLineName);
	}
	
	public ChattingMessage selectMessageByNum(int rowNum){
		return session.selectOne("chatting.selectMessageByNum", rowNum);
	}
	
	public List<ChattingMessage> selectAllMessage(){
		return session.selectList("chatting.selectAllMessage");
	}
}
